package com.yonyou.gmmc.service.pointactivity.service;

import com.yonyou.gmmc.common.service.BaseService;
import com.yonyou.gmmc.service.pointactivity.dao.TtWinnerRecordMapper;
import com.yonyou.gmmc.service.pointactivity.model.*;

import org.apache.log4j.Logger;
import org.marketing.common.event.BizEvent;
import org.marketing.common.event.EventPointActivityDTO;
import org.marketing.common.event.enums.EventConstant;
import org.marketing.common.util.MapConverUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dnys on 2017/9/18.
 */
@Service(value = "ttWinnerRecordService")
@Transactional
public class TtWinnerRecordService {

	private Logger logger = Logger.getLogger(TtWinnerRecordService.class);

	@Autowired
	private TtWinnerRecordMapper ttWinnerRecordMapper;
	@Autowired
	private BaseService eventService;

	public List<Map<String, Object>> queryWinnerAttribute(String date) {
		return ttWinnerRecordMapper.queryWinnerRecordByDate(date);
	}

	public int insertWinnerRecord(TtWinnerRecord ttWinnerRecord) {
		return ttWinnerRecordMapper.insert(ttWinnerRecord);
	}

	public String queryVIN(String openid) {
		return ttWinnerRecordMapper.queryVIN(openid);
	}

	public void recordVisit(Map<String, Object> param) {
		ttWinnerRecordMapper.recordVisit(param);
	}

	public int queryRecordByVIN(Map<String, Object> maps) {
		return ttWinnerRecordMapper.queryRecordByVIN(maps);
	}

	public List<PrizeCountBo> queryRecordReport() {
		return ttWinnerRecordMapper.queryRecordReport();
	}

	public String queryUserByOpenid(String openid) {
		return ttWinnerRecordMapper.queryUserByOpenid(openid);
	}

	public String queryDiffDateStart() {
		return ttWinnerRecordMapper.queryDiffDateStart();
	}

	public String queryDiffDateEnd() {
		return ttWinnerRecordMapper.queryDiffDateEnd();
	}

	/**
	 * 保存抽奖纪录，发送消息
	 * 
	 * @param ttWinnerRecord
	 * @return
	 */
	@Transactional
	public int insertWinnerRecord(TtWinnerRecord ttWinnerRecord, Map<String, Object> recordMap, String openid) throws Exception {
		int count = ttWinnerRecordMapper.insertWinnerRecord(ttWinnerRecord);
		logger.info(ttWinnerRecord.getIfprize());
		if ("1".equals(ttWinnerRecord.getIfprize())) {
			logger.info("开始调用MQ");
			// 根据openid查询userid
			String userid = ttWinnerRecordMapper.queryUserByOpenid(openid);
			logger.info("======开始调用MQuserid:" + userid);
			if (!StringUtils.isEmpty(userid)) {
				BizEvent event = new BizEvent();
				event.setEventBizType(EventConstant.EventBizType.POINT_ACTIVITY);
				event.setEventBizStep(EventConstant.EventBizStep.POINT_ACTIVITY_GET_POINT);
				EventPointActivityDTO dto = new EventPointActivityDTO();
				dto.setChangeNum(Long.valueOf(String.valueOf(recordMap.get("prizepoint"))));
				dto.setUserId(Integer.valueOf(userid));
				event.setEventData(MapConverUtil.PO2Map(dto));
				eventService.sendEvent2MQ(event);
				logger.info("======MQ调用结束");
			}
		}
		return count;
	}

	public void saveYearFeedMsg(TmFiveYearFeedMsg tmFiveYearFeedMsg){
		ttWinnerRecordMapper.saveYearFeedMsg(tmFiveYearFeedMsg);
	}

	public TmFiveYearFeedMsg queryMsgSencond(String openId) {
		return ttWinnerRecordMapper.queryMsgSencond(openId);
	}

	public TmKeep queryTmkeep(String openId) {
		return  ttWinnerRecordMapper.queryTmkeep(openId);
	}

    public List<TmKeepCountVo> queryTmKeepCountByDate() {
		return ttWinnerRecordMapper.queryTmKeepCountByDate();

    }
}
